﻿/*
 * 冗余数据处理 
 * 1. 判断记录是否已经存在
 * 2. 对双职工的判断 
 * 
 */
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace HousingDistribution
{
    class RedundantrDataProc
    {
        static public RedundantrDataProc Instance()
        {
            if (instance == null)
                instance = new RedundantrDataProc();

            return instance;
        }

        public PersonInfo GetMateInfoInMySql(PersonInfo info)
        {
            string mateSex;
            if (info.sex.Contains("男"))
                mateSex = "女";
            else
                mateSex = "男";

            StringBuilder sqlCheck = new StringBuilder("select * from person where name='");
            sqlCheck.Append(info.mateInfo.name);
            sqlCheck.Append("' and sex='");
            sqlCheck.Append(mateSex);
            sqlCheck.Append("' and unit ='");
            sqlCheck.Append(info.mateInfo.unit);
            sqlCheck.Append("'");

            PersonInfo invalidInfo = new PersonInfo();

            try
            {
                DataSet ds = DBProc.Instance().ReadData(sqlCheck.ToString());

                if (ds != null && ds.Tables.Count > 0)
                {
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count == 1)
                    {
                        return parser.ParsePerson(dt.Rows[0]);
                    }
                    else if (dt.Rows.Count > 1)
                    {
                        System.Console.WriteLine("双职工筛查：配偶数量多于一个!!");
                        return invalidInfo;
                    }
                    else
                        return invalidInfo;
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                //发生异常时，则认为记录存在，防止错误记录入库
                return invalidInfo;
            }

            return invalidInfo;
        }

        private RedundantrDataProc()
        {
        }

        static RedundantrDataProc instance;
        ObjectParser parser = new PersonParserFromMySql();
    }
}
